<?php

namespace App\Http\Controllers\Agent;


use Illuminate\Http\Request;
use  App\Http\Controllers\Controller;
use App\Rad_manage;
use App\Http\Controllers\VerifyCodeController;
use App\Common\ErrorDefine;
use App\Common\MyCrypt;

class AgentController extends Controller
{

    private  $verifycode;
    private  $mycrtpy;

    /**
     * Create a new controller instance.
     *
     * @return void
     */

    public function __construct(VerifyCodeController $VerifyCode)
    {
        $this->verifycode = $VerifyCode;
        $this->mycrtpy = new MyCrypt();
    }



    /**
     *诚邀代理注册
     */

    public function InviteAgentRegister(Request $request)
    {

        //  验证用户
        if(!$request->has('username') || empty($request->input('username')) || !preg_match("/^[a-zA-Z0-9]{1}([a-zA-Z0-9]|[._,+-@#$%^&*()~`]){2,20}$/", $request->input('username')))
        {
            self::ShowError(ErrorDefine::NAME_NOT_EXIST);
            return;
        }

        $where = [];
        $where['username'] = $request->input('username');

        $manage = Rad_manage::where('username',$where['username'])->first();

        if($manage != NULL)
        {
            self::ShowError(ErrorDefine::USER_IS_EXIST);
            return;
        }

        //  验证 手机号
        if(!$request->has('mobile') || empty($request->input('mobile')))
        {
            self::ShowError(ErrorDefine::PHONE_NOT_EXIST);
            return;
        }
        $where['mobile'] = $request->input('mobile');

        if(!$request->has('code') || empty($request->input('code')) || !preg_match("/^[0-9]{4}$/", $request->input('code')) )
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }
        $code = $request->input('code');

        // 验证码验证
        if($this->verifycode->Check($where['mobile'],$code) !=1)
        {
            return self::ShowError('10001','无效的验证码');
        }


        //  验证 密码
        if(!$request->has('password') || empty($request->input('password')) || !preg_match("/^[a-zA-Z0-9]{1}([a-zA-Z0-9]|[._,+-@#$%^&*()~`]){2,20}$/", $request->input('password')))
        {
            self::ShowError(ErrorDefine::ILLEGAL_PASSWORD);
            return;
        }
        $where['password'] = $request->input('password');


        $where['password'] = $this->mycrtpy->encrypt('niumokeji-agent,'.$where['password']);



        $where['middle-day'] = 10;  //  基础天卡
        $where['middle-week'] = 49; //  基础周卡
        $where['middle-month'] = 49;//  基础月卡

        $where['height-day'] = 12;  //  高级天卡
        $where['height-week'] = 60; //  高级周卡
        $where['height-month'] = 169;// 高级月卡

        $manage_id = Rad_manage::insertGetId($where);

        $token = md5($manage_id.'niumokeji'.time());
        $manage = Rad_manage::find($manage_id);
        $manage->token = $token;
        $manage->save();

        return self::ShowMessage();



    }




























































}